﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class AQGZSZModule : BaseModule
    {
        public AQGZSZModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            this.tsBtnNew.Visible = false;
            this.tsBtnDelete.Visible = false;
            this.tsBtnImportExcel.Visible = false;
            this.tsBtnDownTemplate.Visible = false;
            this.tsBtnSave.Enabled = false;

            ToolStripButton tsBtnModifyPwd = new ToolStripButton("修改告知平台退出密码", tsBtnModify.Image);
            tsBtnModifyPwd.Click += new EventHandler(tsBtnModifyPwd_Click);
            this.tsMain.Items.Insert(tsMain.Items.IndexOf(tsBtnClose), tsBtnModifyPwd);
            ToolStripSeparator tsSpt = new ToolStripSeparator();
            this.tsMain.Items.Insert(tsMain.Items.IndexOf(tsBtnClose), tsSpt);
        }

        private void tsBtnModifyPwd_Click(object sender, EventArgs e)
        {
            using (AQGZPTPwdModifyForm form = new AQGZPTPwdModifyForm())
            {
                form.ShowDialog();
                if (form.DialogResult == DialogResult.OK)
                {
                    MessageBox.Show("修改成功！", "确定");
                }
            }
        }

        protected override void OnInit()
        {
            base.OnInit();
            this.htmlEditor.ReadOnly = true;
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();
            DataTable dtContent = GetSafetyInfomSettringTable();
            if (dtContent == null || dtContent.Rows.Count <= 0)
            {
                InitSafetyInformSetting();
                dtContent = GetSafetyInfomSettringTable();
            }
            if (dtContent != null && dtContent.Rows.Count > 0)
            {
                htmlEditor.BodyInnerHTML = dtContent.Rows[0]["INFORMCONTENT"].ToString();
            }
        }

        private DataTable GetSafetyInfomSettringTable()
        {
            return DataAccess.ExecuteDataTable("select INFORMCONTENT from T_SAFETY_INFORM_SETTING where ORGCODE = '" + Session.CurrentOrgCode + "'");
        }

        private void InitSafetyInformSetting()
        {
            DataAccess.ExecuteNonQuery("insert into T_SAFETY_INFORM_SETTING(INFORMCONTENT, UPDATEMAN, UPDATEDATE, PASSWORD, ORGCODE)" +
                " values('', '" + Session.CurrentUserName + "', sysdate, '1234', '" + Session.CurrentOrgCode + "')");
        }

        protected override void OnModify()
        {
            base.OnModify();
            if (tsBtnModify.Enabled)
            {
                tsBtnModify.Enabled = false;
                tsBtnRefresh.Enabled = false;
                tsBtnSave.Enabled = true;

                htmlEditor.ReadOnly = false;
            }
        }

        protected override void OnSave()
        {
            base.OnSave();

            string strContent = htmlEditor.BodyInnerHTML;
            string strSql = "update T_SAFETY_INFORM_SETTING set INFORMCONTENT = :INFORMCONTENT, UPDATEMAN = :UPDATEMAN, UPDATEDATE = SYSDATE where ORGCODE = :ORGCODE";
            DataAccess.ExecuteNonQuery(strSql, new OracleParameter(":INFORMCONTENT", strContent),
                new OracleParameter(":UPDATEMAN", Session.CurrentUserName), new OracleParameter(":ORGCODE", Session.CurrentOrgCode));

            MessageBox.Show("保存成功！", "确认");
        }

        protected override void OnAfterSave()
        {
            base.OnAfterSave();
            if (tsBtnSave.Enabled)
            {
                tsBtnSave.Enabled = false;
                tsBtnRefresh.Enabled = true;
                tsBtnModify.Enabled = true;

                htmlEditor.ReadOnly = true;
            }
        }

        protected override void OnBeforeClose(ref bool _canClose)
        {
            base.OnBeforeClose(ref _canClose);
            if (tsBtnSave.Enabled)
            {
                if (MessageBox.Show("修改尚未保存，是否关闭？", "确认", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    _canClose = false;
                }
            }
        }
    }
}
